import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

import approvals from './modules/approvals'
import departments from './modules/departments'
import employees from './modules/employees'
import permissions from './modules/permissions'
import attendances from './modules/attendances'
import salarys from './modules/salarys'
import settings from './modules/settings'
import social_securitys from './modules/social_securitys'

// 动态路由配置，权限控制，不同权限看的的页面不同
export const asyncRoutes = [
  // departments: 组织架构，公司人员架构
  departments,
  // settings：角色管理
  settings,
  // employees：员工管理，给员工分配角色
  employees,
  // permissions：权限管理， 配置各种权限点
  permissions,
  // approvals：审批管理
  approvals,
  // attendances：考勤
  attendances,
  // salarys：工资
  salarys,
  // social_securitys：社保
  social_securitys
]

// 静态路由配置，项目中每个用户都可以访问的功能
// 按需导出，相当于导出一个变量
export const constantRoutes = [
  {
    path: '/login',
    // 路由懒加载，只要访问到这个组件，才会去加载
    component: () => import('@/views/login/index'),
    hidden: true // 为true不会在菜单栏展示
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/import',
    component: Layout,
    redirect: '/import',
    children: [
      {
        path: '',
        name: 'import',
        component: () => import('@/views/employees/import.vue'),
        hidden: true,
        meta: { title: '导入excel', icon: 'import' }
      }
    ]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

// 创建路由实例，给routes属性赋值
const createRouter = () =>
  new Router({
    // 控制路由滚动行为  滚动到顶部
    scrollBehavior: () => ({ y: 0 }),
    // 动态路由,静态路由合并,做路由控制这里不能写死...asyncRoutes
    routes: [...constantRoutes]
  })

const router = createRouter()

// 重置路由函数
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
